package com.wjk.cms.controller;


import com.wjk.cms.common.Result;
import com.wjk.cms.pojo.Card;
import com.wjk.cms.pojo.Category;
import com.wjk.cms.pojo.User;
import com.wjk.cms.service.CardService;
import com.wjk.cms.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController extends AuthenticatedController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CardService cardService;







    @PostMapping("/list")
    public Result list(

    ) {
        List<Category> list =categoryService.list();
        return Result.ok().add("data", list);
    }

    @PostMapping("/getInfo")
    public Result getInfo(
            @RequestParam(value = "id",required = true)Long id
    ) {
        Category category1 =null;

        category1  =categoryService.getById(id);


        return Result.ok().add("category1", category1);
    }

    //查询经销商 已经领取的卡 的类型 和数量
    @PostMapping("/ownList")
    public Result ownList(

    ) {
        User currentUser = getCurrentUser();
        List<Category> list =categoryService.list();
        List<Category> resList = new ArrayList<>();
        for (Category category : list) {
            List<Card> cards = cardService.list(currentUser.getId(), "","1", category.getId()+"",null,null,null,-1, null);
            if(cards!=null && cards.size()>0){
                category.setLeftNum(cards.size());
                resList.add(category);
            }
        }

        return Result.ok().add("data", resList);
    }

}
